package com.xudd.third.interceptor;

import com.xudd.third.common.Constants;
import com.xudd.third.common.SpringContextUtils;
import com.xudd.third.model.User;
import org.springframework.web.servlet.HandlerInterceptor;
import org.thymeleaf.context.IExpressionContext;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

public class LoginInterceptor implements HandlerInterceptor {
	@Override
	public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
		HttpSession session = request.getSession();
		User user = (User) session.getAttribute(Constants.SESSION_USER);
		if (user != null){
			return true;
		}else {
			response.sendRedirect(request.getContextPath()+"/login?"+ SpringContextUtils.getRequestUrl( request));
		}
		return false;
	}
}
